package com.mxpio.erp.equipment.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import com.mxpioframework.jpa.query.Operator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import com.mxpio.erp.common.equipment.entity.EquipmentUpkeepPlanDetail;
import com.mxpio.erp.common.equipment.service.EquipmentUpkeepPlanDetailService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.policy.CrudContext;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;


@Tag(name = "EquipmentUpkeepPlanDetailController}", description = "点检计划明细")
@RestController
@RequestMapping("/erp/equipment/upkeep/plan/detail/")
public class EquipmentUpkeepPlanDetailController {

    @Autowired
    private EquipmentUpkeepPlanDetailService equipmentUpkeepPlanDetailService;


    @GetMapping("list")
    @Operation(summary = "点检计划明细列表", description = "获取点检计划明细列表（无分页）", method = "GET")
    public Result<List<EquipmentUpkeepPlanDetail>> list(@Parameter(description = "查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentUpkeepPlanDetail> list = equipmentUpkeepPlanDetailService.list(EquipmentUpkeepPlanDetail.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "点检计划明细列表", description = "获取点检计划明细列表（分页）", method = "GET")
    public Result<Page<EquipmentUpkeepPlanDetail>> page(@Parameter(description = "查询参数") Criteria criteria, @Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo - 1, pageSize);
        Page<EquipmentUpkeepPlanDetail> pageResult = equipmentUpkeepPlanDetailService.listPage(EquipmentUpkeepPlanDetail.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取点检计划明细列表", description = "根据ID获取点检计划明细列表", method = "GET")
    public Result<EquipmentUpkeepPlanDetail> getById(@PathVariable(name = "id", required = true) String id) {
        EquipmentUpkeepPlanDetail equipmentUpkeepPlanDetail = equipmentUpkeepPlanDetailService.getById(EquipmentUpkeepPlanDetail.class, id);
        return Result.OK(equipmentUpkeepPlanDetail);
    }

    @RequestMapping("add")
    @Operation(summary = "保存点检计划明细", description = "保存点检计划明细")
    public Result<EquipmentUpkeepPlanDetail> add(@RequestBody EquipmentUpkeepPlanDetail equipmentUpkeepPlanDetail) {
        equipmentUpkeepPlanDetailService.addDetail(equipmentUpkeepPlanDetail);
        return Result.OK(equipmentUpkeepPlanDetail);
    }

    @RequestMapping("addByCategory")
    @Operation(summary = "保存点检计划明细-按类别添加", description = "保存点检计划明细-按类别添加")
    public Result<?> addByCategory(@RequestBody EquipmentUpkeepPlanDetail planDetail) {
        equipmentUpkeepPlanDetailService.addDetailByCategory(planDetail.getPlanId(), planDetail.getCategoryId());
        return Result.OK("添加成功！");
    }

    @PutMapping("edit")
    @Operation(summary = "编辑点检计划明细", description = "编辑点检计划明细", method = "PUT")
    public Result<EquipmentUpkeepPlanDetail> edit(@RequestBody EquipmentUpkeepPlanDetail equipmentUpkeepPlanDetail) {
        equipmentUpkeepPlanDetailService.update(equipmentUpkeepPlanDetail);
        return Result.OK(equipmentUpkeepPlanDetail);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除点检计划明细", description = "删除点检计划明细,多个id用英文逗号分隔", method = "DELETE")
    public Result<EquipmentUpkeepPlanDetail> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for (String key : id) {
            equipmentUpkeepPlanDetailService.delete(EquipmentUpkeepPlanDetail.class, key);
        }
        return Result.OK();
    }


}
